﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.Apps.ChiX
{
    /// <summary>
    /// Implements the ChiX stock list as a dictionary, keyed by the umtf
    /// </summary>
    public class ChiXClient
    {
        public const string FILE_CHIX_SYMBOLS = "http://www.chi-xeurope.com/CHIXTSL.txt";

        const char SEPERATOR_ROW = '\n';
        const char SEPERATOR_COL = '|';

        const int SYMBOL_NUM_COL = 15;

        const int SYMBOL_PRI_NAME = 2;
        const int SYMBOL_PRI_RIC = 3;
        const int SYMBOL_PRI_BBG = 4;
        const int SYMBOL_PRI_ISIN = 5;
        const int SYMBOL_PRI_MIC = 7;
        const int SYMBOL_PRI_UMTF = 10;
        const int SYMBOL_PRI_IDX = 11;

        const int SYMBOL_LOC_SYM = 1;
        const int SYMBOL_LOC_CCY = 6;
        const int SYMBOL_LOC_RIC = 8;
        const int SYMBOL_LOC_BBG = 9;
        const int SYMBOL_LOC_TICK = 13;

        const int TICK_NUM_COL = 4;

        const int TICK_NAME = 1;
        const int TICK_MIN = 2;
        const int TICK_SIZE = 3;

        public enum ListingKeyType
        {
            ReutersRIC,
            BloombergBBG,
            UMTF
        }


        public static string CompareTheMeerkat(string meerkat)
        {
            if (meerkat == "MTAA")
                return "XMIL";
            else if (meerkat == "XVTX")
                return "XSWX";

            return meerkat;
        }


        /// <summary>
        /// Returns the current set of Chix listings from the chix website
        /// </summary>
        /// <param name="keyType"></param>
        /// <param name="listings"></param>
        /// <returns></returns>
        public static bool TryGetChiXListings(ListingKeyType keyType, out Dictionary<string, ChiXListing> listings)
        {
            listings = new Dictionary<string, ChiXListing>();

            try
            {
                System.Net.WebClient request = new System.Net.WebClient();

                string[] rows = request.DownloadString(FILE_CHIX_SYMBOLS).Split(SEPERATOR_ROW);

                int end = rows.Length - 2;

                for (int start = 1; start < end; start++)
                {
                    ChiXListing listing = new ChiXListing();

                    string[] data = rows[start].Trim().Split(SEPERATOR_COL);

                    if (data.Length != SYMBOL_NUM_COL)
                        return false;

                    // Currency               
                    string currency = data[SYMBOL_LOC_CCY];
                    if (currency == "GBp" || currency == "GBX")
                        currency = "GBP";
                    listing.Currency = currency;

                    // MarketMIC
                    listing.MarketMIC = CompareTheMeerkat(data[SYMBOL_PRI_MIC]);

                    // Index
                    listing.Index = data[SYMBOL_PRI_IDX].Replace(" ", "");

                    listing.UMTF = data[SYMBOL_PRI_UMTF];
                    listing.ISIN = data[SYMBOL_PRI_ISIN];
                    listing.Name = data[SYMBOL_PRI_NAME];

                    listing.BBGPrimary = data[SYMBOL_PRI_BBG];
                    listing.BBGSecondary = data[SYMBOL_LOC_BBG];

                    listing.RICPrimary = data[SYMBOL_PRI_RIC];
                    listing.RICSecondary = data[SYMBOL_LOC_RIC];

                    string key;
                    switch(keyType)
                    {
                        case ListingKeyType.BloombergBBG:
                            key = listing.BBGPrimary;
                            break;
                        case ListingKeyType.ReutersRIC:
                            key = listing.RICPrimary;
                            break;
                        default:
                            key = listing.UMTF;
                            break;
                    }
                    listings.Add(key, listing);
                }
            }
            catch
            {
                return false;
            }

            return true;
        }




    }
}
